package com.admin.servlet;

import com.admin.entity.Admin;
import com.admin.service.IAdminService;
import com.admin.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminLoginServlet", value = "/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        IAdminService adminService = new AdminServiceImpl();
        boolean result = adminService.adminLogin(new Admin(adminname, password));
        if (result) {
            request.getSession().setAttribute("adminname", adminname);
            request.getSession().setAttribute("msg", "");
//            request.getRequestDispatcher("/UserListServlet").forward(request, response);
            response.sendRedirect("/UserListServlet");
        } else {
            request.getSession().setAttribute("msg", "账号或密码错误！");
            request.getRequestDispatcher("Admin/adminLogin.jsp").forward(request, response);

        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
